package com.runen.infinitemovement.widget;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.PathEffect;
import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

import com.runen.infinitemovement.R;
import com.runen.infinitemovement.model.entity.UserClockInfoEntity;

import java.util.List;


public class TimeLineItemDecoration extends RecyclerView.ItemDecoration {

    private int mCircleSize = 6;//圆圈的半径
    private Paint mPaint;//画笔
    private int mPaintSize = 2;//画笔宽度
    private String mPaintColor = "#FFE8E7E4";//画笔默认颜色
    private int mCircleSizew = 15;
    private Paint mPaintw;//画笔
    private int mPaintSizew = 1;//画笔宽度
    private String mPaintColorw = "#FFE8E7E4";//画笔默认颜色

    private Bitmap bitmap;
    private Bitmap bitmap2;
    private List<UserClockInfoEntity> userClockInfoEntities;
    private Resources resources;

    public TimeLineItemDecoration(Resources resources, List<UserClockInfoEntity> response) {
        mPaint = new Paint();
        mPaint.setStrokeWidth(mPaintSize);
        mPaint.setColor(Color.parseColor(mPaintColor));
        userClockInfoEntities = response;
        this.resources = resources;
        mPaintw = new Paint();
        mPaintw.setStrokeWidth(mPaintSizew);
        mPaintw.setStyle(Paint.Style.STROKE);
        PathEffect effects = new DashPathEffect(new float[]{5, 5, 5, 5}, 1);
        mPaintw.setPathEffect(effects);
        mPaintw.setColor(Color.parseColor(mPaintColorw));
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        Rect rect = new Rect(100, 0, 30, 0);//使item从outRect中右移200px
        outRect.set(rect);
    }

    @Override
    public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(canvas, parent, state);
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = parent.getChildAt(i);
//            boolean isClock=parent.getAdapter()
            int left = child.getLeft();
            int top = child.getTop();
            int bottom = child.getBottom();
            int right = child.getRight();

            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            int bottomMargin = params.bottomMargin;//防止在item根布局中设置marginTop和marginBottom
            int topMargin = params.topMargin;

            int leftX = left / 2;//轴线的x轴坐标
            int height = child.getHeight();//item的高度，不包含Margin

            if (childCount == 1) {
                canvas.drawLine(leftX, top, leftX, bottom - height / 2, mPaint);
            } else {
                if (i == 0) {
                    //  canvas.drawLine(leftX, top + topMargin/2, leftX, top + topMargin/2, mPaint);
                    canvas.drawLine(leftX, top + topMargin / 2 + 84, leftX, bottom + bottomMargin, mPaint);
                } else if (i == childCount - 1) {
                    canvas.drawLine(leftX, top - topMargin, leftX, bottom - height / 2, mPaint);
                    canvas.drawLine(leftX, top + height / 2, leftX, bottom + bottomMargin, mPaint);
                } else {
                    canvas.drawLine(leftX, top - topMargin, leftX, bottom - height / 2, mPaint);
                    canvas.drawLine(leftX, top + height / 2, leftX, bottom + bottomMargin, mPaint);
                }
            }
            // 虚线圆
//            canvas.drawCircle(leftX, top + topMargin / 2 + 70, mCircleSizew, mPaintw);
//            canvas.drawCircle(leftX, top + topMargin / 2 + 70, mCircleSize, mPaint);
//            if (userClockInfoEntities.get(i).checkPointTime.equals("未打卡")) {
//                bitmap = BitmapFactory.decodeResource(resources, R.mipmap.ico_item_clock_point_normal);
//                bitmap2 = BitmapFactory.decodeResource(resources, R.mipmap.ico_small_point_normal);
//                canvas.drawBitmap(bitmap, leftX - 20, top + topMargin / 2 + 80, mPaint);
//            } else {
//                bitmap = BitmapFactory.decodeResource(resources, R.mipmap.ico_item_clock_point);
//                bitmap2 = BitmapFactory.decodeResource(resources, R.mipmap.ico_small_point);
//                canvas.drawBitmap(bitmap, leftX - 27, top + topMargin / 2 + 80, mPaint);
//            }

            canvas.drawBitmap(bitmap2, leftX - 10, top + topMargin / 2 + 255, mPaint);
        }
    }
}